package cn.itheima.web;

import cn.itheima.dao.UserDao;
import cn.itheima.domain.User;
import cn.itheima.service.UserService;
import cn.itheima.service.UserServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author: cuisensen
 * data: 2018/7/5.
 * time: 23:50.
 */
@WebServlet("/repassword")
public class RepasswordServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String password=request.getParameter("password");
        String newpassword=request.getParameter("newpassword");
        String renewpassword=request.getParameter("renewpassword");
        session.setAttribute("includePage","resuccess.jsp");
        if(!newpassword.equals(renewpassword)||!user.getUpassword().equals(password)){
            request.setAttribute("result","失败");
        }else {
            user.setUpassword(newpassword);
            UserService us=new UserServiceImp();
            us.updateUser(user);
            request.setAttribute("result","成功");
        }
        request.getRequestDispatcher(request.getContextPath()+"/index.jsp").forward(request,response);
    }
}
